import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

function route (path, file, name, children) {
  return {
    exact: true,
    path,
    name,
    children,
    component: () => import('./views' + file)
  }
}

const routes = [
  route("/login",'/Login',"Login"),// /login路径，路由到登录组件
  {
    path:"/", // 根路径，路由到 Layout组件
    component: () => import('./views/Layout'),
    redirect:"/login",
    children:[ // 其它所有组件都是 Layout的子组件
      route("/index/dashboard","/Dashboard","Dashboard"),
      // 定义的是路径hash值与组件位置的映射关系
      // 第一个参数：路径hash值，第二个参数：组件的位置，相对于src/views，第三个参数是组件名称
      route("/item/category",'/item/Category',"Category"),
      route("/item/brand",'/item/brand/Index',"Brand"),
      route("/item/list",'/item/goods/Index',"Goods"),
      route("/item/specification",'/item/specification/Index',"Specification"),
      route("/user/list",'/user/List',"UserList"),
      route("/trade/order",'/trade/Order',"Order"),
      route("/auth/services",'/auth/Services',"Services"),
      route("/util/upload",'/util/Upload',"upload"),

    ]
  }
]


const router = new VueRouter({
  routes
})


// 设置路由导航卫士,通过判断是否存在token,对网页访问权限进行设置
router.beforeEach((to, from, next) => {
  // to表示将要访问的地址
  // from表示从哪个路径跳转而来
  // next是放行函数  next()放行    next('/path')强制跳转
  if (to.path === '/login') return next();
  const tokenStr = window.localStorage.getItem('token');
  if (!tokenStr||tokenStr!=='pass') return next('/login');
  next()
})

export default router
/*
new Router({
  routes : [
    route("/login",'/Login',"Login"),// /login路径，路由到登录组件
    {
      path:"/", // 根路径，路由到 Layout组件
      component: () => import('./views/Layout'),
      redirect:"/login",
      children:[ // 其它所有组件都是 Layout的子组件
        route("/index/dashboard","/Dashboard","Dashboard"),
        // 定义的是路径hash值与组件位置的映射关系
        // 第一个参数：路径hash值，第二个参数：组件的位置，相对于src/views，第三个参数是组件名称
        route("/item/category",'/item/Category',"Category"),
        route("/item/brand",'/item/brand/Index',"Brand"),
        route("/item/list",'/item/goods/Index',"Goods"),
        route("/item/specification",'/item/specification/Index',"Specification"),
        route("/user/list",'/user/List',"UserList"),
        route("/trade/order",'/trade/Order',"Order"),
        route("/auth/services",'/auth/Services',"Services"),
        route("/util/upload",'/util/Upload',"upload"),

      ]
    }
  ]
})*/

